package com.parleys.android.server.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.parleys.android.server.util.ParleysServiceUtils;
import com.parleys.server.domain.News;
import com.parleys.server.domain.types.NewsType;
import com.parleys.server.dto.ChannelOverviewDTO;
import com.parleys.server.dto.PresentationOverviewDTO;
import com.parleys.server.frontend.service.ParleysService;
import com.parleys.server.frontend.service.PresentationsCriteria;

@Controller
@RequestMapping("/channels")
public class ChannelsController extends BaseController {

	@Autowired
	private ParleysService parleysService;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ChannelOverviewDTO getChannel(@PathVariable("id") long channelId) {
    	return parleysService.getChannelOverviewDTO(channelId);
    }
    
    @RequestMapping(value = "/{id}/news", method = RequestMethod.GET)
    public List<News> getNewsForChannel(@PathVariable("id") long channelId) {
        return parleysService.getNews(NewsType.CHANNEL, channelId, 0, Integer.MAX_VALUE).getOverviews();
    }

    @RequestMapping(value = "/{id}/news/start/{from}", method = RequestMethod.GET)
    public List<News> getNewsFromForChannel(@PathVariable("id") long channelId,
    		@PathVariable("from") final long from) {
        List<News> result = this.getNewsForChannel(channelId);
        ParleysServiceUtils.filterEntity(result, from);
        return result;
    }

    @RequestMapping(value = "/{id}/presentations", method = RequestMethod.GET)
    public List<PresentationOverviewDTO> getPresentations(@PathVariable("id") long channelId) {
    	PresentationsCriteria criteria = new PresentationsCriteria();
    	criteria.setChannelId(channelId);
    	criteria.setIndex(0);
    	criteria.setPaging(Integer.MAX_VALUE);
    	return (List<PresentationOverviewDTO>) parleysService.getPresentationsOverview(criteria);
    }
    
}
